package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.SettingsPersonalBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/6/26.
 * 修改密码
 */

public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.cet_original)
    ClearableEditText cetOriginal;
    @BindView(R.id.tv_line2)
    TextView tvLine2;
    @BindView(R.id.cet_mima_one)
    ClearableEditText cetMimaOne;
    @BindView(R.id.tv_line)
    TextView tvLine;
    @BindView(R.id.text1)
    ImageView text1;
    @BindView(R.id.cet_mima_two)
    ClearableEditText cetMimaTwo;
    @BindView(R.id.rl_tijiao)
    RelativeLayout rlTijiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        setView();

    }

    private void setView() {
        cetOriginal.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetMimaOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetMimaTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });

    }
    private void editTextIsNo() {
        if (TextUtils.isEmpty(cetOriginal.getText())||TextUtils.isEmpty(cetMimaOne.getText())||TextUtils.isEmpty(cetMimaTwo.getText())){
            rlTijiao.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(cetOriginal.getText())&&!TextUtils.isEmpty(cetMimaOne.getText())&&!TextUtils.isEmpty(cetMimaTwo.getText())){
            rlTijiao.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }
    @OnClick({R.id.iv_back, R.id.rl_tijiao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_tijiao:
                if (TextUtils.isEmpty(cetOriginal.getText())||TextUtils.isEmpty(cetMimaOne.getText())||TextUtils.isEmpty(cetMimaTwo.getText())){
                    return;
                }

                if (cetMimaOne.getText().toString().equals(cetMimaTwo.getText().toString())){
                    //提交修改密码
                    if (cetMimaOne.getText().length()<6||cetMimaOne.getText().length()<6){
                        ToastFactory.getToast(activity,"密码长度过短").show();
                        return;
                    }
                    initData();
                }else{
                    ToastFactory.getToast(activity,"两次密码输入不正确").show();
                    return;
                }
                break;
        }
    }

    private void initData() {
        Log.i("11", "onNext: "+spUtils.getString(AppConstant.USER_NAME));
        Log.i("11", "onNext: "+spUtils.getString(AppConstant.TOKEN));
        HttpMethods.getInstanceCenter().info.changePassword(spUtils.getString(AppConstant.TOKEN),spUtils.getString(AppConstant.USER_NAME),cetOriginal.getText().toString(),cetMimaOne.getText().toString(),cetMimaTwo.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingsPersonalBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingsPersonalBean settingsPersonalBean) {
                        Log.i("11", "onNext: "+settingsPersonalBean.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("11", "onNext: "+e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
